package univer.oko.search.queryBuilders;

import univer.oko.search.searchParamsMaps.ISearchParamsMap;
import univer.oko.search.searchParamsMaps.PaymentSearchParamsMap;
import univer.oko.utilities.DateUtilities;

import java.util.Date;
import java.util.Set;

/**
 * Created by Sergey Kharlamov.
 * Date: 01.08.2006
 * Time: 22:40:24
 */
public class PaymentEditorPaymentQueryBuilder implements IQueryBuilder {
    private StringBuilder builder;

    public String createQuery(ISearchParamsMap map) {
        builder = new StringBuilder();
        Set<String> keys = map.getKeys();
        for (String param : keys) {
            ProcessMapping(param, map.getValue(param), builder);
        }
        if (builder.length() != 0) {
            builder.delete(builder.length() - "AND ".length(), builder.length());
        }
        return builder.toString();
    }

    private void ProcessMapping(String key, Object value, StringBuilder builder) {

        if (key.equals(PaymentSearchParamsMap.PAYMENT_TYPE_ID)) {
            builder.append("[ref_paymenttype] = ").append(value);
        } else if (key.equals(PaymentSearchParamsMap.STUDENT_FIRST_NAME)) {
            builder.append("[studname] like ('" + value + "%')");
        } else if (key.equals(PaymentSearchParamsMap.STUDENT_LAST_NAME)) {
            builder.append("[studsurname] like ('" + value + "%')");
        } else if (key.equals(PaymentSearchParamsMap.PAY_DATE_FROM)) {
            builder.append("[paydate] >= ").append(DateUtilities.databaseFormat((Date) value));
        } else if (key.equals(PaymentSearchParamsMap.PAY_DATE_TO)) {
            builder.append("[paydate] <= ").append(DateUtilities.databaseFormat((Date) value));
        } else if (key.equals(PaymentSearchParamsMap.FACULTY)) {
            builder.append("[faculty_name] = '" + value + "'");
        } else if (key.equals(PaymentSearchParamsMap.DIRECTION_NAME)) {
            builder.append("[Direction_name] = '" + value + "'");
        } else if (key.equals(PaymentSearchParamsMap.SPECIALITY_NAME)) {
            builder.append("[Speciality_name] = '" + value + "'");
        }
        builder.append(" AND ");
    }

}
